package com.qf.watcher;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Id;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

public class TestWatcher {
    private static ZooKeeper zooKeeper;
    private static final String ZKADDR = "10.9.12.200:8601,10.9.12.200:8602,10.9.12.200:8603";
    private static CountDownLatch countDownLatch = new CountDownLatch(1);//用作阻塞的, 1 的意思是只需有一个地方执行了放行就放行所有阻塞的地方

    public static void main(String[] args) throws Exception {

        zooKeeper=new ZooKeeper(ZKADDR, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("这个方法之心过了"+Thread.currentThread().getName());
                Event.KeeperState state = watchedEvent.getState();
                String path = watchedEvent.getPath();
                Event.EventType type = watchedEvent.getType();

                countDownLatch.countDown();
            }
        });

        countDownLatch.await();

//        增删改查

        String s = zooKeeper.create("/04cacheservice", "sss".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);





        System.in.read();
    }
}
